package com.song.controller;

import com.song.pojo.Dept;
import com.song.pojo.Result;
import com.song.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result getAllDept(){
     List<Dept> depts = deptService.findAllDepts();
     return Result.success(depts);
    }
    @DeleteMapping
    public Result deleteDeptById(Integer id){
        int i = deptService.deleteDeptById(id);
        System.out.println(i);
        if(i==1){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }

    }
    @PostMapping
    public Result addDept(@RequestBody Dept dept ){
        int i = deptService.addDept(dept.getName());
        if(i==1){
            return Result.success();
        }else {
            return Result.error("添加失败");
        }

    }
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        int i = deptService.updateDept(dept);
        if(i==1){
            return Result.success();
        }else {
            return Result.error("修改失败");
        }
    }
    @GetMapping("/{id}")
    public Result getDeptById(@PathVariable Integer id){
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }
}
